package com.bs.action;

import java.util.List;
import net.sf.json.JSONArray;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import com.bs.bean.Hospital;
import com.bs.service.HospitalService;

@SuppressWarnings("serial")
@ParentPackage("struts-default")
@Namespace("/hospital")
public class HospitalAction extends ActionBase {
	private Hospital hospital;
	private HospitalService hospitalService;
	public Hospital getHospital() {
		return hospital;
	}
	public void setHospital(Hospital hospital) {
		this.hospital = hospital;
	}
	
	public HospitalService getHospitalService() {
		return hospitalService;
	}
	public void setHospitalService(HospitalService hospitalService) {
		this.hospitalService = hospitalService;
	}
	@Action("save")
	public void save() throws Exception{
		try {
			this.hospitalService.save(hospital);
			response.getWriter().write(this.successInfo("添加医院信息成功"));
		} catch (Exception e) {
			response.getWriter().write(this.errorInfo("添加医院信息失败，可能医院名称已存在"));
			e.printStackTrace();
		} 
	}
	
	@Action("delete")
	public void delete() throws Exception{
		try {
			String ids = (String) request.getParameter("ids");
			int rows = this.hospitalService.delete(ids);
			if(rows >= 0){
				response.getWriter().write(this.successInfo("删除医院信息成功"));
			}
		} catch (Exception e) {
			response.getWriter().write(this.errorInfo("医院信息正在被使用，不可删除"));
			e.printStackTrace();
		}
	}
	
	@Action("query")
	public void query() throws Exception{
		try {
			List<Hospital> list = this.hospitalService.query(hospital);
			response.getWriter().write(JSONArray.fromObject(list).toString());
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
}
